<template>
	<!--  <el-tree :props="props" :load="loadNode" lazy @node-click="hadleClick">
  </el-tree> -->
	<el-tree :props="props" :load="loadNode" lazy @node-click="hadleClick">
	</el-tree>
</template>
<script>
	import {
		getAllSchools
	} from "@/api/bbs_user/bbs_user";
	import {
		listPlateCate,
		listPlateTree
	} from "@/api/bbs_plate/plate.js";

	export default {
		data() {
			return {
				root: [{
					name: "校园列表"
				}],
				query: {
					schoolId: "",
					cateId: "",
				},
				plates: [],
				props: {
					label: "name",
					children: "zones",
					isLeaf: (data,node)=>{
						if(node.level==4){ //四级为叶子节点
							return true
						}
					},
				},
			};
		},
		methods: {
			async loadNode(node, resolve) {
				console.log(node.level);
				if (node.level === 0) {
					return resolve(this.root); //this.schools
				}
				if (node.level === 1) return resolve(this.schools);
				if (node.level === 2) return resolve(this.cates);
				if (node.level === 3) {
					console.log(this.query);
					this.query.cateId = node.data.cateid;
					this.query.schoolId = node.parent.data.schoolid;
					listPlateTree(this.query).then((res) => {
						console.log("plate是", res);
						this.plates = res;
						console.log('加载完成');
						for (var i = 0; i < this.plates.length; i++) {
							this.plates[i].isLeaf = true
						}
						return resolve(this.plates);
					});
				} else {
					return resolve([]);
				}

				// setTimeout(() => {
				//   const data = [
				//     {
				//       name: "leaf",
				//       leaf: true,
				//     },
				//     {
				//       name: "zone",
				//     },
				//   ];

				//   resolve(data);
				// }, 500);
			},
			hadleClick(item, node, el) {
				console.log(node);
				if (node.level === 3) {
					console.log(this.query);
					this.query.cateId = node.data.cateid;
					this.query.schoolId = node.parent.data.schoolid;
					this.getPlateTree();
				}
			},
			getSchools() {
				getAllSchools().then((response) => {
					console.log(response.data);
					this.schools = response.data;
					this.schools = this.schools.map((x) => {
						x["name"] = x["schoolname"];
						return x;
					});
				});
			},
			getCates() {
				listPlateCate().then((res) => {
					console.log(res);
					res = res.map((x) => {
						x.name = x.catename;
						return x;
					});
					this.cates = res;
				});
			},
			getPlateTree() {
				listPlateTree(this.query).then((res) => {
					console.log("plate是", res);
					this.plates = res;
				});
			},
		},
		mounted() {
			this.getSchools();
			this.getCates();
			this.getPlateTree();
		},
	};
</script>
